package com.gin.kotlin.gf2gamecommunity.viewmodel

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.gin.kotlin.enums.ComplaintReason
import com.gin.kotlin.form.ComplaintForm
import com.gin.kotlin.gf2gamecommunity.app.App
import com.gin.kotlin.gf2gamecommunity.base.livedata.CachedPagingData
import com.gin.kotlin.gf2gamecommunity.base.livedata.ParamCachedLiveData
import com.gin.kotlin.gf2gamecommunity.base.util.copyText
import com.gin.kotlin.gf2gamecommunity.callback.AbstractNothingCallback
import com.gin.kotlin.gf2gamecommunity.pagingsource.TopicInfoPagingSource
import com.gin.kotlin.gf2gamecommunity.routes.TopicListRoute
import com.gin.kotlin.response.Res
import com.gin.kotlin.response.topic.TopicDetail
import com.gin.kotlin.response.topic.TopicInfo
import com.gin.kotlin.response.topic.ToppedTopic

/**
 * 主题列表ViewModel
 * @property topicList 主题列表
 * @property topicDetail 主题详情
 * @property toppedTopic 置顶帖
 * @constructor
 */
class TopicListViewModel : ViewModel() {

    val topicList = CachedPagingData<TopicInfo, TopicListRoute>(scope = viewModelScope) { TopicInfoPagingSource(it) }

    val topicDetail = ParamCachedLiveData<Res<TopicDetail>, TopicDetail, Int>(
        requestMethod = { id -> api().get(id) },
        bodyTransform = { it.data },
    )

    val toppedTopic = ParamCachedLiveData<Res<List<ToppedTopic>>, List<ToppedTopic>, Int>(
        requestMethod = { id -> api().getTop(id) },
        bodyTransform = { it.data },
    )

    init {
        Log.i(TAG, "实例化")
    }


    /**
     * 点赞主题
     * @param topicId 主题ID
     * @param onSuccess 成功回调
     */
    fun like(topicId: Int, onSuccess: () -> Unit = {}) = api().like(topicId).enqueue(object : AbstractNothingCallback() {
        override fun onSuccess(message: String) {
            App.toast(message)
            onSuccess()
        }
    })

    /**
     * 点踩主题
     * @param topicId 主题ID
     * @param onSuccess 成功回调
     */
    fun bad(topicId: Int, onSuccess: () -> Unit = {}) = api().bad(topicId).enqueue(object : AbstractNothingCallback() {
        override fun onSuccess(message: String) {
            App.toast(message)
            onSuccess()
        }
    })


    /**
     * 分享主题并复制地址
     * @param topicId 主题ID
     * @param onSuccess 成功回调
     */
    fun share(topicId: Int, onSuccess: () -> Unit = {}) = api().share(topicId).enqueue(object : AbstractNothingCallback() {
        override fun onSuccess(message: String) {
            App.INSTANCE.copyText("${App.HOST}/threadInfo?id=$topicId", "已复制主题链接")
            onSuccess()
        }
    })

    /**
     * 收藏主题
     * @param topicId 主题ID
     * @param onSuccess 成功回调
     */
    fun favor(topicId: Int, onSuccess: () -> Unit = {}) = api().favor(topicId).enqueue(object : AbstractNothingCallback() {
        override fun onSuccess(message: String) {
            App.toast(message)
            topicDetail.fetch(topicId, true)
            onSuccess()
        }
    })

    /**
     * 举报主题
     * @param topicId Int
     * @param reason ComplaintReason
     * @param onSuccess () -> Unit
     */
    fun complaint(topicId: Int, reason: ComplaintReason, onSuccess: () -> Unit = {}) = api().complaint(ComplaintForm(topicId, reason)).enqueue(object : AbstractNothingCallback() {
        override fun onSuccess(message: String) {
            App.toast(message)
            onSuccess()
        }
    })


    private fun api() = App.INSTANCE.api.topicApi

    companion object {
        private val TAG = TopicListViewModel::class.java.simpleName
    }
}